<?php

class CityHelper extends Helper {

    public function __construct() {
        
    }

    public function provinceAll($bank_id = 0,$new=false) {
        Cache::Set('public', 'ajax', 1800);
        if ((!$province_cache = Cache::Get('province_all'.$bank_id)) ||$new == true) {
            $cityModel = $this->LoadModel('Public');
            $screen_arr = $bank_id >0? array('`display` = ?' => 3,'`admittance_bank` LIKE (?) OR `admittance_bank`="'.$bank_id.'" OR `admittance_bank` LIKE "'.$bank_id.',%" OR `admittance_bank` LIKE "%,'.$bank_id.'"' =>'%,'.$bank_id.',%') : array('`display` = ?' => 3);
            $data = $cityModel->getList('city', '*', $screen_arr);
            Cache::Save('province_all'.$bank_id, $data);
        } else {
            $data = $province_cache;
        }
        return $data;
    }

    public function cityAll($province,$bank_id = 0,$new = false) {
        Cache::Set('public', 'ajax', 1800);
        if ((!$city_cache = Cache::Get('city_all'.$bank_id)) || $new == true) {
            $cityModel = $this->LoadModel('Public');
            $screen_arr = $bank_id >0? array('`display` = ?' => 2,'`admittance_bank` LIKE (?) OR `admittance_bank`="'.$bank_id.'" OR `admittance_bank` LIKE "'.$bank_id.',%" OR `admittance_bank` LIKE "%,'.$bank_id.'"' =>'%,'.$bank_id.',%') : array('`display` = ?' => 2);
            $city = $cityModel->getList('city', '*', $screen_arr);
            Cache::Save('city_all'.$bank_id, $city);
        } else {
            $city = $city_cache;
        }
        $select_city = array();
        foreach ($city as $key => $val) {
            if ($val['state'] == $province) {
                $select_city[] = $val;
            }
        }
        return $select_city;
    }

    public function getArea($city,$bank_id = 0 , $new = false) {
        $model = $this->LoadModel('Public');
        $city_row = $model->getOneRow('city', '*', array('`display` = ?' => 2, '`city` = ?' => $city));
        if (empty($city_row))
            return false;
        Cache::Set('public', 'ajax', 1800);
        if ((!$area_cache = Cache::Get($city_row['sz_code'] . '_area'.$bank_id)) || $new == true) {
            $code = substr($city_row['sz_code'], 0, 4);
            $screen_arr = $bank_id > 0? array('`display` = ?' => 1, '`sz_code` LIKE ?' => $code . '%','`admittance_bank` LIKE (?) OR `admittance_bank`="'.$bank_id.'" OR `admittance_bank` LIKE "'.$bank_id.',%" OR `admittance_bank` LIKE "%,'.$bank_id.'"' =>'%,'.$bank_id.',%') : array('`display` = ?' => 1, '`sz_code` LIKE ?' => $code . '%');
            $area = $model->getList('city', '*', $screen_arr);
            Cache::Save($city_row['sz_code'] . '_area'.$bank_id, $area);
        } else {
            $area = $area_cache;
        }
        return $area;
    }

    public function regroupCity($bank_id=0) {
        $province = $this->provinceAll();
        $re_arr = array();
        foreach ($province as $k => $v) {
            $city = $this->cityAll($v['state'],$bank_id);
            $re_city = array();
            foreach ($city as $key => $val) {
                $area = $this->cityAll($v['']);
                $re_city['o_' . $val['city']] = array(
                    't' => $val['city']
                );
            }
            $re_arr['o_' . $v['state']] = array(
                't' => $v['state'],
                'c' => $re_city
            );
        }
        return $re_arr;
    }
    
    public function getAll($bank_id = 0,$new = false){
        $province_arr = $this->provinceAll($bank_id,$new);
        if(!empty($province_arr)){
            foreach($province_arr as $k=>$v){
                $city_arr[$v['state']] = $this->cityAll($v['state'],$bank_id,$new);
            }
        }
        if(!empty($city_arr)){
            foreach($city_arr as $key => $val){
                foreach($val as $x => $y){
                    $area_arr[$y['city']] = $this->getArea($y['city'],$bank_id,$new);
                }   
            }
        }
        $msg_arr['province'] = $province_arr;
        $msg_arr['city'] = $city_arr;
        $msg_arr['area'] = $area_arr;
        return $msg_arr;
    }

    public function takeoutId($bank_id = 0, $new = false){
        $data = $this->getAll($bank_id, $new);
        if(!empty($data['province'])){
            foreach($data['province'] as $xk => $xv){
                $id_arr[] = $xv['id'];
            }
        }
        if(!empty($data['city'])){
            foreach($data['city'] as $k => $v){
                foreach($v as $ck => $cv){
                    $id_arr[] = $cv['id'];
                }
            }
        }
        if(!empty($data['area'])){
            foreach($data['area'] as $x => $y){
               foreach ($y as $xk => $yk) {
                   $id_arr[] = $yk['id'];
               }
            }
        }
        return $id_arr;

    }

    public function admittanceEdit($close,$open,$bank_id){
        $model = $this->LoadModel('Public');
        $model->beginTransaction();
        try{
            if(!empty($close)){
                foreach($close as $k => $v){
                    $data = $model->getOneRow('city','*',array('`id` =?' => $v));
                    if($data['admittance_bank'] == $bank_id){
                        $model->update('city',array('admittance_bank'=>''),array('`id` =?' =>$v));
                    }else{
                        $admittance_arr = explode(',',$data['admittance_bank']);
                        $key = array_search($bank_id,$admittance_arr);
                        unset($admittance_arr[$key]);
                        $admittance = implode(',',$admittance_arr);
                        $model->update('city',array('admittance_bank'=>$admittance),array('`id` =?' =>$v));
                    }
                }
            }
            if(!empty($open)){
                foreach($open as $x => $y){
                    $data = $model->getOneRow('city','*',array('`id` =?' => $y));
                    if(empty($data['admittance_bank'])){
                        $admittance_data = $bank_id;
                    }else{
                        $bank_arr = explode(',',$data['admittance_bank']);
                        $bank_arr[count($bank_arr)] = $bank_id;
                        $admittance_data = implode(',',$bank_arr);
                    }
                    $model->update('city',array('admittance_bank'=>$admittance_data),array('`id` =?' =>$y));
                }
            }
            $model->commit();
            return array('success' => true ,'msg' => '保存成功');
        } catch (Exception $ex) {
            $model->rollBack();
            return array('success' => false, 'msg' => '保存异常'.$ex);
        }
    }
    
}